//
//  CoreDataViewController.m
//  Dean
//
//  Created by Maranan  on 2/25/14.
//  Copyright (c) 2014 SRPH. All rights reserved.
//

#import "CoreDataViewController.h"
#import "AppDelegate.h"


@interface CoreDataViewController ()

@property (weak, nonatomic) IBOutlet UITextField *CategoryName;
@property (weak, nonatomic) IBOutlet UIButton *AddTransactionButton;
@property (weak, nonatomic) IBOutlet UILabel *DataCategory;
@property (weak, nonatomic) UIPickerView *CategoryPickerView;
- (IBAction)AddTransaction:(id)sender;

@end

@implementation CoreDataViewController

- (IBAction)AddTransaction:(id)sender {
    
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *expensesCategory;
    expensesCategory = [NSEntityDescription
                        insertNewObjectForEntityForName:@"ExpenseCategory" inManagedObjectContext:context];
    [expensesCategory setValue:_CategoryName.text forKey:@"name"];
    //_CategoryName.text = @"";
    NSError *error;
    [context save:&error];
    
    NSEntityDescription *label = [NSEntityDescription entityForName:@"ExpenseCategory" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity: label];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", _CategoryName.text];
    [request setPredicate:pred];
    NSManagedObject *matches = nil;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if ([objects count] == 0) {
        _DataCategory.text = @"No Matches";
    } else {
        matches = objects[[objects count] - 1];
        _DataCategory.text = [matches valueForKey:@"name"];
    }
    _CategoryName.text = @"";
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 200;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *returnStr = @"Default";
    if (pickerView == _CategoryPickerView)
    {
        returnStr = [_listPickerArray objectAtIndex:row];
    }
    
    return returnStr;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
        return _listPickerArray.count;
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    _listPickerArray = [NSArray arrayWithObjects: @"Food", @"Transport", @"Bill", nil];
    _CategoryPickerView.delegate = self;
    _CategoryPickerView.dataSource = self;
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end